<?php

namespace app\modules\Customer\models;

use Yii;
use app\modules\Manage\models\User;
/**
 * This is the model class for table "customer".
 *
 * @property int $id
 * @property string $name 公司名称
 * @property string $en_name 英文名称
 * @property string $address 公司地址
 * @property string $en_address 英文公司地址
 * @property string $person 联系人姓名
 * @property string $en_person 英文联系人姓名
 * @property string $job 职务
 * @property string $en_job 职务英文
 * @property string $phone 联系电话
 * @property string $day_time 付款天数
 * @property string $remark 备注
 * @property int $status 1正常  2禁用
 * @property int $user_id 创建人ID
 * @property string $created_at 创建时间
 * @property string $updated_at
 */
class Customer extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'customer';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['name', 'en_name', 'address', 'en_address'], 'required'],
            [['status', 'user_id'], 'integer'],
            [['created_at', 'updated_at'], 'safe'],
            [['name', 'en_name', 'address', 'en_address','payment','en_payment'], 'string', 'max' => 255],
            [['person', 'en_person', 'phone'], 'string', 'max' => 32],
            [['job', 'en_job'], 'string', 'max' => 64],
            [['remark'], 'string', 'max' => 512],
        ];
    }

     // 保存前处理
    public function beforeSave($insert) {//{{{

        if($insert){
            $this->created_at = date("Y-m-d H:i:s");
            $this->user_id    = Yii::$app->user->id;
        }
        

        return parent::beforeSave($insert);
    }//}}}

     //获取录入人
    public function getUser(){
        return $this->hasOne(User::className(), ['id' => 'user_id']);
    }


    // 获取所有有效客户
    public static function all(){//{{{
        
        $model = Customer::find()->where(['status'=>1])->all();     
        
        return $model;  
    }//}}}

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app.customer', 'ID'),
            'name' => Yii::t('app.customer', 'Name'),
            'en_name' => Yii::t('app.customer', 'En Name'),
            'address' => Yii::t('app.customer', 'Address'),
            'en_address' => Yii::t('app.customer', 'En Address'),
            'person' => Yii::t('app.customer', 'Person'),
            'en_person' => Yii::t('app.customer', 'En Person'),
            'job' => Yii::t('app.customer', 'Job'),
            'en_job' => Yii::t('app.customer', 'En Job'),
            'phone' => Yii::t('app.customer', 'Phone'),
            'payment' => Yii::t('app.customer', 'Payment'),
            'en_payment' => Yii::t('app.customer', 'En Payment'),
            'remark' => Yii::t('app.customer', 'Remark'),
            'status' => Yii::t('app.customer', 'Status'),
            'user_id' => Yii::t('app', 'User ID'),
            'created_at' => Yii::t('app', 'Created At'),
            'updated_at' => Yii::t('app', 'Updated At'),
        ];
    }
}
